<?php

ini_set('include_path', '../');

require_once("lib/exception/duplicate_instance_exception.php");
require_once("lib/exception/internal_error_exception.php");
require_once("lib/exception/instance_not_found_exception.php");

require_once("util/configuration_parametters.php");

require_once("model/article_article_category/to/article_article_category_to.php");
require_once("model/article_article_category/dao/article_article_category_dao_factory.php");

require_once("model/article/to/article_to.php");
require_once("model/article/dao/article_dao_factory.php");

require_once("model/article_category/to/article_category_to.php");
require_once("model/article_category/dao/article_category_dao_factory.php");

require_once("model/autor/to/autor_to.php");
require_once("model/autor/dao/autor_dao_factory.php");

class Article_article_category_dao_test {

	private $dao;
		
	private $autor;
	private $article;
	private $article_category;
		
	public function __construct() {		
	}
	
	public function before_test() {
		try {
			$this->autor = (object) Autor_dao_factory::singleton()->get_dao()->create(new Autor_to('xxxxxxxxxxxx','password','email'));
			$date = date("Y-m-d H:i:s");
			$this->article = (object) Article_dao_factory::singleton()->get_dao()->create(new Article_to(0, 'title', $this->autor->get_login(), $date, 'open', 0));
			$this->article_category = (object) Article_category_dao_factory::singleton()->get_dao()->create(new Article_category_to(0,'title', 'description', 'xxxxxxxxxxxx', 0));
			$this->article_category2 = (object) Article_category_dao_factory::singleton()->get_dao()->create(new Article_category_to(0,'title2', 'description', 'xxxxxxxxxxxx', 0));
			$this->dao = Article_article_category_dao_factory::singleton()->get_dao();
		}
		catch (Exceprion $e) {
			throw $e;
		}
	}
	
	public function run_test() {
		try {
			$article_article_category_to = new Article_article_category_to($this->article->get_number(), $this->article_category->get_number());
			$article_article_category2_to = new Article_article_category_to($this->article->get_number(), $this->article_category2->get_number());

			
			//create
			$this->dao->create($article_article_category2_to->get_article_number(), 
									$article_article_category2_to->get_article_category_number());
			$article_article_category_to = (object) $this->dao->create($article_article_category_to->get_article_number(), 
																		$article_article_category_to->get_article_category_number());		
			echo 'create article_article_category: ' . $article_article_category_to->to_string() . '<br/>';	

			//create existent 
			try {
				$this->dao->create($article_article_category_to->get_article_number(), 
									$article_article_category_to->get_article_category_number());
			}
			catch (Duplicate_instance_exception $e) {
				echo $e->getMessage() . '<br/>';
			}
			
			//exist inexistent 
			if (!$this->dao->exist(9999, 9999))
				echo 'Not exist article_article_category 9999, 9999<br/>';
			else 
				throw new Exception('Error: Exist inexistent article article category');
			
			//exist existent 
			$this->dao->exist($article_article_category_to->get_article_number(), 
									$article_article_category_to->get_article_category_number());
			echo 'exist article_article_category: ' . $article_article_category_to->to_string() . '<br/>';
			
			//find inexistent 
			try {
				$this->dao->find_by_number_and_category_article(9999, 9999);
				throw new Exception('Found inexistent');	
			}
			catch (Instance_not_found_exception $e) {
				echo $e->getMessage() . '<br/>';
			}
			
			//find existent 
			$aux_article_article_category_to = $this->dao->find_by_number_and_category_article($article_article_category_to->get_article_number(), 
																								$article_article_category_to->get_article_category_number());	
				if ($article_article_category_to->equals($aux_article_article_category_to)) 
					echo 'found article_article_category: ' . $article_article_category_to->to_string() . '<br/>';
				else
					echo 'not equal article_article_category inserted';
					
			//find all category for article inexistent
			try {
				$category_list = $this->dao->find_by_number_article(9999);
				throw new Exception('Found categories for article inexistent');
			}
			catch (Instance_not_found_exception $e) {
				echo $e->getMessage() . '</br>';			
			}
						
					
			//find all category for article existent
			$category_list = $this->dao->find_by_number_article($article_article_category_to->get_article_number());
				echo 'Article categories: </br>';
				foreach($category_list as $category)
					echo 'category number: ' . $category . '</br>';
									
			//remove inexistent 	
			try {
				$this->dao->remove(9999, 9999);
				throw new Exception('Removed inexistent');
			}
			catch (Instance_not_found_exception $e) {
				echo $e->getMessage() . '</br>';
			}
			
			//remove existent 
			$this->dao->remove($article_article_category_to->get_article_number(), 
								$article_article_category_to->get_article_category_number());
			echo 'remove article_article_category: ' . $article_article_category_to->to_string() . '<br/>';

		}
		catch (Duplicate_instance_exception $e) {
			echo $e->getMessage();
		}
		catch (Internal_error_exception $e) {
			echo $e->getMessage();
		}
		catch (Instance_not_found_exception $e) {
			echo $e->getMessage();
		}
		catch (Exception $e) {
			echo $e->getMessage();
		}
	}
	
	public function after_test() {
		Article_category_dao_factory::singleton()->get_dao()->remove($this->article_category->get_number());
		Article_dao_factory::singleton()->get_dao()->remove($this->article->get_number());
		Autor_dao_factory::singleton()->get_dao()->remove($this->autor->get_login());
		
	}
}

//test
$test = new Article_article_category_dao_test();

$test->before_test();
$test->run_test();
$test->after_test();

?>